package org.itboys.core.service.mysql.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.coupon.SundayCouponOrderMapper;
import org.itboys.core.dto.product.SundaySkuDto;
import org.itboys.core.entity.mongo.*;
import org.itboys.core.entity.mysql.SundayCouponOrder;
import org.itboys.core.service.mongo.web.*;
import org.itboys.core.util.NumberUtil;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author：贾琦磊
 * @Date：2018/8/30
 * @Description：促销单
 */
@Service
public class SundayCouponOrderService extends BaseMySqlService<SundayCouponOrder>{
    private static final String ORDER_PREFIX="CouponOrder";
    @Autowired
    private SundayCouponOrderMapper couponOrderMapper;
    @Autowired
    private SundayCouponOrderSKUService couponOrderSKUService;
    @Autowired
    private SundayCouponOrderUserAreaService couponUserAreaService;
    @Autowired
    private SundayCouponOrderSubstractService couponOrderSubstractService;
    @Autowired
    private SundaySkuService skuService;
    public void save(Long id, SundayCouponOrder couponOrder, SundayCouponOrderSubstract couponOrderSubstracts,
                     List<Integer> userAreas, List<Long> SKUs){

        Long couponOrderId;
        if(LongUtil.isNotNull(id)){
            couponOrder.setId(id);
            couponOrderId=id;
            couponOrderMapper.update(couponOrder);
        }else {
            couponOrder.setNumber(NumberUtil.generateNumber(ORDER_PREFIX));
            couponOrderMapper.insert(couponOrder);
            couponOrderId=couponOrder.getId();
        }
        List<SundayCouponOrderUserArea> couponOrderUserAreas=new ArrayList<>();
        List<SundayCouponOrderSKU> couponOrderSKUS=new ArrayList<>();
        for (Integer userType:userAreas
                ) {
            SundayCouponOrderUserArea couponOrderUserArea=new SundayCouponOrderUserArea( couponOrderId,  userType);
            couponOrderUserAreas.add(couponOrderUserArea);
        }
        for (Long skuId:SKUs
                ) {
            SundayCouponOrderSKU couponSKU=new SundayCouponOrderSKU(couponOrderId,skuId);
            couponOrderSKUS.add(couponSKU);
        }
        couponOrderSubstracts.setCouponId(couponOrderId);
        //更新该优惠券下所有的sku范围
        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("couponId",couponOrder.getId());
        couponOrderSKUService.delete(param);
        couponOrderSKUService.batchSave(couponOrderSKUS);
        //更新所有的用户范围
        couponUserAreaService.delete(param);
        couponUserAreaService.batchSave(couponOrderUserAreas);
        //更新所有的满减规则
        couponOrderSubstractService.delete(param);
        couponOrderSubstractService.save(couponOrderSubstracts);
    }

    public SundayCouponOrder findOne(Long couponId){
        if(LongUtil.isNotZreo(couponId)){
            SundayCouponOrder coupon = couponOrderMapper.find(couponId);
            Map<String,Object> param=new HashMap<>();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("couponId",couponId);
            List<SundayCouponOrderSKU> couponOrderSKUs= couponOrderSKUService.list(param);
            List<SundayCouponOrderUserArea> couponUserAreas = couponUserAreaService.list(param);
            List<SundayCouponOrderSubstract> couponOrderSubstracts = couponOrderSubstractService.list(param);
            if(ListUtil.isNotNull(couponOrderSubstracts)){
                coupon.setSubstract(couponOrderSubstracts.get(0));
            }
            List<Integer> userTypes=new ArrayList<>();
            List<Long> skuIds=new ArrayList<>();
            for(SundayCouponOrderUserArea userArea:couponUserAreas){
                userTypes.add(userArea.getUserType());
            }
            for(SundayCouponOrderSKU sku:couponOrderSKUs){
                skuIds.add(sku.getSkuId());
            }
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("ids",skuIds );
            List<SundaySkuDto> sundaySkuDtos = skuService.packageDtoMuilt(skuService.select(param));
            for (SundaySkuDto dto:sundaySkuDtos
                    ) {
                for (SundayCouponOrderSKU couponOrderSKU:couponOrderSKUs
                        ) {
                    if(couponOrderSKU.getSkuId().equals(dto.getId())){
                        couponOrderSKU.setProductName(dto.getProductName());
                        couponOrderSKU.setSkuNumber(dto.getNumber());
                    }
                }

            }
            coupon.setCouponSKUS(couponOrderSKUs);
            coupon.setUserTypes(userTypes);
            return coupon;
        }
        return null;
    }
    public PageResult<SundayCouponOrder> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request,"name");
        return super.selectPage(param);
    }
}
